import React, { useState, useEffect } from 'react';
import { Form, Button, Space, notification } from 'antd';
import { goBack, componentsMap } from '@/utils/util';
import API from '@/api/xjindex.js';
import { useModel } from 'umi';
import UploadList from '@/components/UploadList';
import moment from 'moment';

const propertyCheck = ({ history }) => {
  const [fields] = useState([]);
  const [form] = Form.useForm();
  const [fileList, setFileList] = useState([]);
  const [assetTwo, setAssetTwo] = useState([]);
  const [orgId, setOrgId] = useState('');
  const [isshow, setIsShow] = useState(false);
  const [orgList, setOrgList] = useState([]);
  const [GDZCLX, setGDZCLX] = useState([]);
  const { initialState } = useModel('@@initialState');
  const { username } = initialState.info;

  form.setFieldsValue({
    createName: username,
    createTime: moment().format('YYYY-MM-DD'),
  });
  const TableList = [
    {
      label: '资产名称',
      name: 'assetName',
      type: 'input',
      rules: [
        {
          required: true,
          message: '请输入资产名称',
        },
      ],
    },
    {
      label: '所属单位',
      name: 'affiliatedUnit',
      type: 'select',
      rules: [
        {
          required: true,
          message: '请选择所属单位名',
        },
      ],
      otherProps: {
        options: orgList,
      },
    },
    {
      label: '资产类型',
      name: 'assetType',
      type: 'select',
      rules: [
        {
          required: true,
          message: '请选择资产类型',
        },
      ],
      otherProps: {
        options: GDZCLX,
      },
    },
    {
      label: '资产型号',
      name: 'assetModel',
      type: 'input',
      rules: [
        {
          required: true,
          message: '请输入资产型号',
        },
      ],
      //   otherProps: {
      //     options: assetTwo,
      //   },
    },
    {
      label: '设备IP',
      name: 'deviceIp',
      type: 'input',
      rules: [
        {
          required: true,
          message: '请输入设备IP',
        },
      ],
      //   otherProps: {
      //     disabled: true,
      //   },
    },
    {
      label: '部署位置',
      name: 'deploymentLocation',
      type: 'input',
      rules: [
        {
          required: true,
          message: '请输入部署位置',
        },
      ],
    },
    {
      label: '采购年限',
      name: 'procurementYears',
      type: 'input',
      rules: [
        {
          required: true,
          message: '请输入采购年限',
        },
        {
          pattern: /^\d/,
          message: '请输入数字',
        },
      ],
    },
    {
      label: '创建人',
      name: 'createName',
      type: 'input',
      otherProps: {
        disabled: true,
      },
    },
    {
      label: '创建时间',
      name: 'createTime',
      type: 'input',
      otherProps: {
        disabled: true,
      },
    },
    {
      label: '备注说明',
      name: 'remarks',
      type: 'textarea',
      isremark: true,
    },
  ];
  const TableMaintain = [
    {
      label: '运维单位',
      name: 'maintenanceUnit',
      type: 'input',
      rules: [
        {
          required: true,
          message: '请输入运维单位',
        },
      ],
    },
    {
      label: '联系人',
      name: 'contacts',
      type: 'input',
      rules: [
        {
          required: true,
          message: '请输入运维单位',
        },
      ],
    },
    {
      label: '联系方式',
      name: 'contactInformation',
      type: 'input',
      rules: [
        {
          required: true,
          message: '请输入运维单位',
        },
        {
          pattern: /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/,
          message: '手机号格式不正确',
        },
      ],
    },
  ];

  const submitFn = (values) => {
    if (values._lastTime) {
      values.lastTime = values._lastTime.format('YYYY-MM-DD');
    }
    values.id = history.location.query.id;
    values.assetFixedFileList = values.file;
    API.modAssetFixed(values).then((res) => {
      if (res) {
        notification.success({
          message: '修改成功',
        });
        goBack();
      }
    });
  };
  const uploadOptions = {
    fileTypes: [
      'application/doc',
      'application/pdf',
      'application/msword',
      'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
      'image/jpg',
      'image/jpeg',
      'image/png',
    ],
    maxSize: 50,
    maxNum: 3,
    accept: [
      '.doc,.docx.,.pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,.jpg,.jpeg,.png',
    ],
    acceptMessage: 'jpg/word/pdf',
  };
  function initList() {
    API.getOrgList()
      .then((res) => {
        if (res) {
          setOrgList(
            res.t.map((item) => {
              return {
                label: item.name,
                value: item.id,
              };
            }),
          );
        }
      })
      .then(() => {
        API.getGDZCLX().then((res) => {
          if (res) {
            const result = res.t.map((item) => {
              return {
                label: item.dictItemName,
                value: item.dictItemType,
              };
            });
            setGDZCLX(result);
          }
        });
      })
      .then(() => {
        API.selectAssetFixedById({ id: history.location.query.id }).then(
          (res) => {
            if (res) {
              form.setFieldsValue({
                ...res.t,
                //   _lastTime: res.t.lastTime ? moment(res.t.lastTime, 'YYYY-MM-DD') : '',
              });
              setOrgId(res.t.orgId);
              setFileList(res.t.assetFixedFileList);
            }
          },
        );
      });
  }

  useEffect(() => {
    initList();
  }, []);
  return (
    <div className="main-x">
      <Form form={form} onFinish={submitFn}>
        <div className="base-title">基本信息</div>
        <div className="bgf">
          <div className="ant-form-inline form-x max-x">
            {TableList.map((item) => {
              return (
                <Form.Item
                  key={item.name}
                  label={item.label}
                  name={item.name}
                  rules={item.rules}
                  className={item.isremark ? 'block' : ''}
                >
                  {componentsMap(item.type, item.otherProps)}
                </Form.Item>
              );
            })}
          </div>
        </div>
        <div className="base-title mt20">运维信息</div>
        <div className="bgf">
          <div className="ant-form-inline form-x max-x">
            {TableMaintain.map((item) => {
              return (
                <Form.Item
                  key={item.name}
                  label={item.label}
                  name={item.name}
                  className={item.isremark ? 'block' : ''}
                  rules={item.rules}
                >
                  {componentsMap(item.type, item.otherProps)}
                </Form.Item>
              );
            })}
          </div>
        </div>
        <div className="base-title mt20">附件信息</div>
        <div className="form-x bgf" style={{ paddingBottom: '16px' }}>
          <Form.Item
            label="附件信息"
            extra="支持jpg/word/pdf格式,单个大小在50M以内,支持多个上传，最多上传3个附件"
            name="file"
            className="block"
          >
            <UploadList
              uploadUrl="/assetFixed/AssetFixedFileUpload"
              downloadUrl="/assetFixed/AssetFixedDownload"
              options={uploadOptions}
              defaultFiles={fileList}
            ></UploadList>
          </Form.Item>
        </div>
        <div className="footer-x">
          <Space size={18}>
            <Button type="primary" htmlType="submit">
              提交
            </Button>
            <Button type="primary" className="cancel-btn" onClick={goBack}>
              返回
            </Button>
          </Space>
        </div>
      </Form>
    </div>
  );
};
export default propertyCheck;
